<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :validatorRules="validatorRules">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="用户名" prop="username">
          <a-input placeholder="请输入用户名" v-decorator="['username', validatorRules.username ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="手机号" prop="mobile">
          <a-input placeholder="请输入手机号" v-decorator="['mobile', validatorRules.mobile ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="类型">
          <a-radio-group name="type" v-decorator="[ 'type',{initialValue: 0 }]" >
            <a-radio :value="0">商户</a-radio>
            <!-- <a-radio :value="1">店员</a-radio> -->
          </a-radio-group>
        </a-form-item>
        <a-form-item v-if="false" :labelCol="labelCol" :wrapperCol="wrapperCol" label="性别">
          <a-radio-group name="sex" v-decorator="[ 'sex',{initialValue: 0 }]"  >
            <a-radio :value="0">男</a-radio>
            <a-radio :value="1">女</a-radio>
          </a-radio-group>
        </a-form-item>
        <!-- <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="实名认证">
          <a-input placeholder="请输入实名认证" v-decorator="['idCard', {}]" />
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="状态">
          <a-radio-group
            name="state"
            v-decorator="[ 'state', {initialValue: 0 }]"
          >
          <!-- :defaultValue="0" -->
            <a-radio :value="0">正常</a-radio>
            <a-radio :value="1">冻结</a-radio>
          </a-radio-group>
        </a-form-item>
        <!-- <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="身份证号" prop="idCard">
          <a-input placeholder="请输入身份证号" v-decorator="['idCard', validatorRules.idCard ]" />
        </a-form-item> -->
        <!-- <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="验证码">
          <a-input placeholder="请输入验证码" v-decorator="['code', validatorRules.code ]" />
        
        </a-form-item> -->
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import { httpAction ,getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import {validatePhone,validateIdNo} from '@/utils/validator'

export default {
  name: 'SellerModal',
 
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        username: { rules: [{ required: true, message: '请输入商户名!' }] },
        mobile: { rules: [{ required: true, message: '请输入商户手机号!' },{validator:validatePhone,trigger:blur}] },
        // idCard: { rules: [{ required: true, message: '请输入身份证号' },{validator:validateIdNo,trigger:blur}] },
        code: { rules: [{ required: true, message: '请输入验证码' }] }
        // type: { rules: [{ required: true}] },
        // sex: { rules: [{ required: true }] },
        // state: { rules: [{ required: true }] },
      },
      url: {
        add: '/seller/web/add',
        edit: '/seller/web/edit'
        ,code:'/seller/getVerificationCode'
      }
    }
  },
  created() {},
  
  methods: {
   
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(this.model, 'name', 'mobile', 'type', 'weixinOpenId', 'sex', 'isDelete', 'idCard', 'state', 'username')
        )
        //时间格式化
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'post'
          }
          let formData = Object.assign(this.model, values)
          //时间格式化

          console.log(formData)
          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style lang="less" scoped>
</style>